﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 是挂上该脚本的物体带上血条
/// </summary>
public class HpSlider : MonoBehaviour {

    private Entity parentEntity;//获取父物体
    private Slider hpSlider;//获取血条的Slider
    private float hpLimit;//血条的上线
    private Vector3 localV = new Vector3(1f, 1f, 1f);
	// Use this for initialization
	void Start () {
        hpSlider =transform.GetChild(0).GetComponent<Slider>();
        parentEntity = transform.parent.gameObject.GetComponent<Entity>();
        if (parentEntity != null)
            hpLimit = parentEntity.HpLimit;
        parentEntity.OnHpChange +=new System.Action<float>( ChangSliderValue);
        
	}
	public void ChangSliderValue(float value)
    {
        hpSlider.value = value / hpLimit;
        if(transform.localScale!=localV)
        {
            transform.localScale = localV;
            if(value!=0)
            StartCoroutine(WaitTrunBack());
        }
    }//改变血量，并且判断当击中的时候显示血条，不被击中的时候隐藏血条
    private IEnumerator WaitTrunBack()
    {
        yield return new WaitForSeconds(2);
        transform.localScale = Vector3.zero;
    }
}
